package com.mall.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.R;
import com.mall.entity.LoginUser;
import com.mall.entity.SmoothMallCart;
import com.mall.entity.SmoothMallProduct;
import com.mall.entity.dto.SmoothMallCartDto;
import com.mall.mapper.SmoothMallCartMapper;
import com.mall.service.SmoothMallCartService;
import com.mall.service.SmoothMallProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class SmoothMallCartServiceImpl extends ServiceImpl<SmoothMallCartMapper, SmoothMallCart> implements SmoothMallCartService {
    @Autowired
    SmoothMallProductService smoothMallProductService;
    @Override
    public boolean addToCart(Integer productId, Integer count) {
        //从上下文中获取用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser principal = (LoginUser) authentication.getPrincipal();
        Integer userId = principal.getSmoothMallUser().getId();
        LambdaQueryWrapper<SmoothMallCart> lambdaQueryWrapper = new LambdaQueryWrapper<SmoothMallCart>()
                .eq(SmoothMallCart::getUserId,userId).eq(SmoothMallCart::getProductId,productId);
        long result = count(lambdaQueryWrapper);
        if (result==0){
            return add(productId, count, userId);
        }else {
            return update(productId, count, userId, lambdaQueryWrapper);
        }
    }

    private boolean update(Integer productId, Integer count, Integer userId, LambdaQueryWrapper<SmoothMallCart> lambdaQueryWrapper) {
        SmoothMallCart cart = getOne(lambdaQueryWrapper);
        Integer quantity = cart.getQuantity();
        //判断库存是否充足
        SmoothMallProduct mallProduct = smoothMallProductService.getById(productId);
        Integer stock = mallProduct.getStock();
        if (stock< count +quantity){
            return false;
        }
        //封装购物车信息

        cart.setProductId(productId);
        cart.setUserId(userId);
        cart.setQuantity(count +quantity);
        //添加购物车信息
        boolean save = saveOrUpdate(cart);
        return save;
    }

    private boolean add(Integer productId, Integer count, Integer userId) {
        //判断库存是否充足
        SmoothMallProduct mallProduct = smoothMallProductService.getById(productId);
        Integer stock = mallProduct.getStock();
        if (stock< count){
            return false;
        }
        //封装购物车信息
        SmoothMallCart smoothMallCart = new SmoothMallCart();
        smoothMallCart.setProductId(productId);
        smoothMallCart.setUserId(userId);
        smoothMallCart.setQuantity(count);
        //添加购物车信息
        boolean save = save(smoothMallCart);
        return save;
    }

    @Override
    public List<SmoothMallCartDto> getListByUserId() {
        //从上下文中获取用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser principal = (LoginUser) authentication.getPrincipal();
        Integer userId = principal.getSmoothMallUser().getId();
        //查询出该用户id的购物车列表
        LambdaQueryWrapper<SmoothMallCart> wrapper = new LambdaQueryWrapper<SmoothMallCart>().eq(SmoothMallCart::getUserId, userId);
        List<SmoothMallCart> list = list(wrapper);
        ArrayList<SmoothMallCartDto> smoothMallCartDtos = new ArrayList<>();
        list.stream().forEach(one->{
            SmoothMallCartDto dto = getDto(one);
            smoothMallCartDtos.add(dto);
        });
        return smoothMallCartDtos;
    }

    @Override
    public boolean deleteCart(Integer productId) {
        //从上下文中获取用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser principal = (LoginUser) authentication.getPrincipal();
        Integer userId = principal.getSmoothMallUser().getId();
        boolean remove = remove(new LambdaQueryWrapper<SmoothMallCart>()
                .eq(SmoothMallCart::getUserId, userId)
                .eq(SmoothMallCart::getProductId, productId));
        return remove;
    }

    //修改选中状态
    @Override
    public List<SmoothMallCartDto> selectForCart(Integer productId, Integer selectId) {
        //从上下文中获取用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser principal = (LoginUser) authentication.getPrincipal();
        Integer userId = principal.getSmoothMallUser().getId();
        SmoothMallCart cart = getOne(new LambdaQueryWrapper<SmoothMallCart>()
                .eq(SmoothMallCart::getUserId, userId)
                .eq(SmoothMallCart::getProductId, productId));
        cart.setSelected(selectId==0?1:0);
        return getListByUserId();
    }

    private SmoothMallCartDto getDto(SmoothMallCart one) {
        SmoothMallProduct product = smoothMallProductService.getById(one.getProductId());
        Integer price = product.getPrice();
        Integer totalPrice=product.getPrice()* one.getQuantity();
        SmoothMallCartDto smoothMallCartDto = new SmoothMallCartDto();
        BeanUtils.copyProperties(one,smoothMallCartDto);
        smoothMallCartDto.setPrice(price);
        smoothMallCartDto.setTotalPrice(totalPrice);
        smoothMallCartDto.setProductName(product.getName());
        smoothMallCartDto.setProductImage(product.getImage());
        return smoothMallCartDto;
    }
}
